package com.yurendao.OSFileSystem.dao;

import javax.swing.JOptionPane;

import com.yurendao.OSFileSystem.bean.FileBean;
import com.yurendao.OSFileSystem.data.DataAttribute;
import com.yurendao.OSFileSystem.MyFAT.FATStructure;
import com.yurendao.OSFileSystem.disk.DiskAttributes;
import com.yurendao.OSFileSystem.disk.MyDisk;
import com.yurendao.OSFileSystem.file.MyFile;

public class FATDao {
	private FATStructure[] myfat;

	private FATDao() {
		this.myfat = new FATStructure[128];
		for (int i = 0; i < this.myfat.length; i++) {
			this.myfat[i] = new FATStructure();
		}

		this.myfat[0].setFatIndex(DataAttribute.FAT_numberOfEnd);
		this.myfat[0].setFatType(DataAttribute.FAT_aDisk);
		DiskAttributes rootAttributes = new DiskAttributes("C");
		MyDisk root = new MyDisk(0, 0, rootAttributes);
		this.myfat[0].setFatContent(root);
		this.myfat[1].setFatIndex(DataAttribute.FAT_numberOfEnd);

		for (int i = DataAttribute.FAT_startNumber; i < this.myfat.length; i++) {
			this.myfat[i].setFatIndex(DataAttribute.FAT_numberOfusable);
		}
	}

	public void addFile(FileBean fileBean) {
		int previous = 0;
		int latter = 0;
		int numberofFAT = fileBean.getMyfileSize();
		// 磁盘有足够的空间用于存储
		// 找到第一个 空闲的磁盘块
		for (int i = DataAttribute.FAT_startNumber; i < this.myfat.length; i++) {
			if (this.myfat[i].getFatIndex() == DataAttribute.FAT_numberOfusable) {
				previous = i;
				// MyFile temp = new
				// MyFile(fileBean.getMyfatherFATNumber(),previous,fileBean.getMyfileContent(),aFileAttributes);

				this.myfat[i].setFatContent(fileBean);
				this.myfat[i].setFatType(DataAttribute.FAT_aFile);
				break;
			}
		}
		// 顺序的为该文件 申请磁盘空间 并在文件分配表中穿创建文件的索引
		// previous：前一个索引的盘块号 latter：后一个索引的盘块号
		for (int i = previous + 1; i < this.myfat.length; i++) {
			// 将最后的盘块的索引 记为255
			if (fileBean.getMyfileSize() == 1) {
				this.myfat[previous].setFatIndex(DataAttribute.FAT_numberOfEnd);
				break;
			}
			if (this.myfat[i].getFatIndex() == DataAttribute.FAT_numberOfusable) {
				latter = i;
				this.myfat[previous].setFatIndex(latter);
				previous = latter;
				numberofFAT--;
			}
		}
	}
	
	public void deleteFile(){
		
	}

	public boolean hasSizeCreateFolder() {
		int myFATNumber = 0;
		int i = 0;
		// 判断 分配表中是否有足够空间创建文件夹 并获取下标
		for (i = DataAttribute.FAT_startNumber; i < this.myfat.length; i++) {
			if (this.myfat[i].getFatIndex() == DataAttribute.FAT_numberOfusable) {
				myFATNumber = i;
				this.myfat[i].setFatIndex(DataAttribute.FAT_numberOfEnd);
				break;
			}
		}
		if (i >= this.myfat.length) {
			return false;
		} else {
			return true;
		}
	}

	public boolean hasSizeCreateFile(int fileSize) {

		int usableNumber = 0;

		for (int i = DataAttribute.FAT_startNumber; i < this.myfat.length; i++) {
			if (this.myfat[i].getFatIndex() == DataAttribute.FAT_numberOfusable)
				usableNumber++;
		}
		if (usableNumber < fileSize) {
			return false;
		} else {
			return true;
		}
	}

	private static FATDao instance;

	public static synchronized FATDao getInstance() {
		if (instance == null) {
			instance = new FATDao();
		}
		return instance;
	}
}
